package xyz.o0o0o.demo.RedisDemo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

public class HessianTest {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Date date = new Date();

		long t1 = System.currentTimeMillis();
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		HessianOutput ho = new HessianOutput(os);
		ho.writeObject(date);
		System.out.println(bytesToHexString(os.toByteArray()));

		ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
		HessianInput hi = new HessianInput(is);
		Object obj = null;
		obj = hi.readObject();
		System.out.println((Date) obj);
		System.out.println(System.currentTimeMillis() - t1);

		long t2 = System.currentTimeMillis();
		ByteArrayOutputStream os1 = new ByteArrayOutputStream();
		Hessian2Output h2o = new Hessian2Output(os1);
		h2o.writeObject(date);
		h2o.flush();
		System.out.println(bytesToHexString(os1.toByteArray()));

		ByteArrayInputStream is1 = new ByteArrayInputStream(os1.toByteArray());
		Hessian2Input h2i = new Hessian2Input(is1);
		Object obj2 = h2i.readObject();
		System.out.println((Date) obj2);
		System.out.println(System.currentTimeMillis() - t2);

		long t3 = System.currentTimeMillis();
		ByteArrayOutputStream os2 = new ByteArrayOutputStream();
		ObjectOutputStream objectOut = new ObjectOutputStream(os2);
		objectOut.writeObject(date);
		System.out.println(bytesToHexString(os2.toByteArray()));

		ByteArrayInputStream is2 = new ByteArrayInputStream(os2.toByteArray());
		ObjectInputStream objectIn;
		Object obj3 = null;
		objectIn = new ObjectInputStream(is2);
		obj3 = objectIn.readObject();
		System.out.println((Date) obj3);
		System.out.println(System.currentTimeMillis() - t3);

	}

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}
}
